package com.yehun.utils;

import com.yehun.enums.CellTypeEnum;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号码辅助类
 * Created by Genius on 2017/5/8 0008.
 */
public class RegexUtils {

    private static boolean match(String regex, String value) {
        return Pattern.matches(regex, value);
    }

    public static boolean isCell(String value) {
        return match("^(13|15|18|14|17)[0-9]{9}$", value);
    }

    public static boolean isUUID(String value) {
        return match("^[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}$", value);
    }

    public static boolean isEmail(String value) {
        return match("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", value);
    }

    public static boolean isIP(String value) {
        return match("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$", value);
    }

    public static CellTypeEnum getCellType(String cell) {
        if(match("\"^(130|131|132|185|186|155|156|176)[0-9]{8}\"", cell)) {
            return CellTypeEnum.Unicom; //联通
        }
        if(match("^(135|136|137|138|139|134|147|150|151|152|154|157|158|159|181|182|183|184|187|188|178)[0-9]{7,8}", cell)) {
            return CellTypeEnum.Unicom; //移动
        }
        if(match("^(100|133|153|180|189|177|170|173)[0-9]{8}", cell)) {
            return CellTypeEnum.Unicom; //电信
        }
        return CellTypeEnum.Unknown;
    }


    public static void main(String[] args) {
        String string = "[/account/login]";
        Pattern pattern = Pattern.compile("(?<=\\[)(.+?)(?=\\])");
        Matcher matcher = pattern.matcher(string);
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}
